package com.erju.utils;


/**
 * 将返回结果封装起来
 *
 * @author erju
 */
public class Result<T> {
    /**
     * 状态码 0代表成功 非0是失败
     */
    private Integer status;
    private String state;
    /**
     * 给前端的返回信息
     */
    private String msg;
    /**
     * 返回对象
     */
    private T data;

    public String getCode() {
        return state;
    }

    public void setCode(String code) {
        this.state = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Result() {
    }

    public Result(T data) {
        this.data = data;
    }

    /**
     * 成功 无data
     *
     * @return Result
     */
    public static Result success() {
        Result result = new Result<>();
        result.setCode("0");
        result.setMsg("成功");
        return result;
    }

    /**
     * 成功 有data
     *
     * @param msg
     * @param <T>
     * @return
     */

    public static <T> Result<T> success(String msg) {
        return success("0",msg,null);
    }
    public static <T> Result<T> success(String msg, T data) {
        return success("0",msg,data);
    }
    public static <T> Result<T> success(String code,String msg, T data) {
        Result<T> result = new Result<>(data);
        result.setCode(code);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }

    /**
     * 失败
     *
     * @param code
     * @param msg
     * @return
     */
    public static Result error(String code, String msg) {
        Result result = new Result();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }
    public static Result error() {
        Result result = new Result();
        result.setCode("1");
        result.setMsg("错误");
        return result;
    }
    public static Result error(String msg) {
        Result result = new Result();
        result.setCode("1");
        result.setMsg(msg);
        return result;
    }

    public static Result fail(Integer status, String msg,String data) {
        Result result = new Result();
        result.setCode(status.toString());
        result.setMsg(msg);
        return result;
    }
}
